package com.stylefeng.guns.rest.modular.cinema;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.cinema.CinemaServiceApi;
import com.stylefeng.guns.api.cinema.vo.*;
import com.stylefeng.guns.api.order.OrderServiceApi;
import com.stylefeng.guns.rest.modular.cinema.vo.CinemaConditionResponseVo;
import com.stylefeng.guns.rest.modular.cinema.vo.CinemaFiledResponeVo;
import com.stylefeng.guns.rest.modular.cinema.vo.CinemaFiledsResponeVo;
import com.stylefeng.guns.rest.modular.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.security.acl.LastOwnerException;
import java.util.List;

/**
 * @ClassName CinemaController
 * @Description TODO
 * @Author zhaoluowei
 * @Date 2019/9/15  9:10
 * @Version 1.0
 */

@Slf4j
@RestController
@RequestMapping(value = "/cinema")
public class CinemaController {

    //明显的特征就是不走缓存
    @Reference(interfaceClass = CinemaServiceApi.class, connections = 10, check = false, cache = "lru")
    private CinemaServiceApi cinemaServiceApi;


    //直接注入 api
    @Reference(interfaceClass = OrderServiceApi.class, check = false)
    private OrderServiceApi orderServiceApi;


    private static final String img_pre = "http://img.meetingshop.cn/";


    @RequestMapping(value = "/getCinemas", method = RequestMethod.POST)
    public ResponseVo getCinemas(CinemaQueryModel cinemaQueryVo) {
        log.info(" getCinemas -->  " + cinemaQueryVo);
        try {
            //根据条件进行筛选
            Page<CinemaVo> cinemas = cinemaServiceApi.getCinemas(cinemaQueryVo);
            //判断是否有满足条件的影院。
            if (cinemas.getRecords() == null || cinemas.getRecords().size() == 0) {
                return ResponseVo.success("没有影院可查");
            } else {
                return ResponseVo.success(cinemas.getCurrent(), (int) cinemas.getPages(), "", cinemas.getRecords());
            }

        } catch (Exception e) {
            //如果出现异常该如何处理
            log.error("获取医院列表异常", e);
            return ResponseVo.serviceFail("查询影院列表异常");
        }
    }

    /**
     * 热点数据 --> 放缓存
     *
     * @param cinemaQueryVo
     * @return
     */
    @RequestMapping(value = "/getCondition", method = RequestMethod.POST)
    public ResponseVo getCondition(CinemaQueryModel cinemaQueryVo) {
        try {
            log.info(" getCondition -->  " + cinemaQueryVo);
            //获取三个结合 封装成一个对象
            List<BrandVo> brands = cinemaServiceApi.getBrands(cinemaQueryVo.getBrandId());
            log.info("brands --> " + brands);
            List<HallTypeVo> hallTypes = cinemaServiceApi.getHallTypes(cinemaQueryVo.getHallType());
            log.info("hallTypes --> " + hallTypes);
            List<AreaVo> areas = cinemaServiceApi.getAreas(cinemaQueryVo.getDistrictId());
            log.info("areas --> " + areas);


            CinemaConditionResponseVo responseVo = new CinemaConditionResponseVo();
            responseVo.setAreaList(areas)
                    .setHallTypeList(hallTypes)
                    .setBrandList(brands);

            return ResponseVo.success(responseVo);

        } catch (Exception e) {
            log.error("影院条件查询异常 --> ", e.getMessage());
            return ResponseVo.serviceFail("影院条件查询异常");
        }


    }


    @RequestMapping(value = "/getFileds", method = RequestMethod.POST)
    public ResponseVo getFileds(Integer cinemaId) {
        log.info(" cinemaId -->  " + cinemaId);
        try {
            CinemaInfoVo cinemaInfo = cinemaServiceApi.getCinemaInfoById(cinemaId);
            List<FilmInfoVo> filmInfoByCinemaId = cinemaServiceApi.getFilmInfoByCinemaId(cinemaId);


            return ResponseVo.success(img_pre, new CinemaFiledsResponeVo(cinemaInfo, filmInfoByCinemaId));
        } catch (Exception e) {
            log.error(" 获取播放场次失败 --> ", e.getMessage());
            return ResponseVo.serviceFail("获取播放场次失败");
        }

    }


    @RequestMapping(value = "/getFiledInfo", method = RequestMethod.POST)
    public ResponseVo getFiledInfo(Integer cinemaId, Integer fieldId) {
        log.info(" cinemaId -->  " + cinemaId);
        log.info(" fieldId -->  " + fieldId);
        try {
            CinemaInfoVo cinemaInfoById = cinemaServiceApi.getCinemaInfoById(cinemaId);
            FilmInfoVo filmInfoByFieldId = cinemaServiceApi.getFilmInfoByFieldId(fieldId);
            HallInfoVo filmFieldInfo = cinemaServiceApi.getFilmFieldInfo(fieldId);


            String soldSeatsByFieldId = orderServiceApi.getSoldSeatsByFieldId(fieldId);
            log.info("走订单查询,查询已售 的座位id -->  " + soldSeatsByFieldId);
            filmFieldInfo.setSoldSeats(soldSeatsByFieldId);


            CinemaFiledResponeVo cinemaFiledResponeVo = new CinemaFiledResponeVo();
            cinemaFiledResponeVo.setCinemaInfo(cinemaInfoById)
                    .setFilmInfo(filmInfoByFieldId)
                    .setHallInfo(filmFieldInfo);


            return ResponseVo.success(img_pre, cinemaFiledResponeVo);
        } catch (Exception e) {
            log.error(" 获取 选座信息 失败 --> ", e.getMessage());
            return ResponseVo.serviceFail("获取 选座信息 失败");
        }
    }


}
